using Framework;
using System.Collections.Generic;

public class SimpleObjectPool<T> where T : class, new()
{
    System.Func<T> creator;
    Stack<T> pool;
    int maxCapacity;
    public SimpleObjectPool(System.Func<T> creator, int capacity = 32)
    {
        this.creator = creator;
        if (capacity < 8)
            capacity = 8;
        pool = new Stack<T>(capacity);
        maxCapacity = capacity * 2;
    }
    public T Get()
    {
        if (pool.Count > 0)
            return pool.Pop();
        return creator();
    }
    public void Put(T handle)
    {
        if (null == handle)
            return;

        if (pool.Count == maxCapacity)
        {
            SLogger.W($"{handle.GetType()} Out Of Pool Range [pooledCount:{pool.Count}] [poolCapacity:{maxCapacity}]");
            return;
        }

        pool.Push(handle);
    }
}
